from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import MultinomialNB
from sklearn.linear_model import LogisticRegression
import pandas
from sklearn.metrics import classification_report


def g_replace(texts):
    new_test = []
    for i in range(len(texts)):
        # 句子去除标点符号
        new_sentence = texts[i].replace(',',' ').replace('，',' ').replace('/',' ').replace('。',' ').replace('、',' ')
        new_test.append(new_sentence)
    print('完成数据预处理！')
    return new_test
# 训练数据
# train_docs = ["this is good", "right and good", "well done", "nice work"]
# train_labels = ["positive", "positive", "positive", "positive"]
train=pandas.read_csv("F:\\pythonProject\\dataset\\train.csv")
test=pandas.read_csv("F:\\pythonProject\\dataset\\test.csv")
train_docs=list(train["text"])
train_docs=g_replace(train_docs)
train_labels=list(train["label"])
# 测试数据
# test_docs = ["Story of a man who has unnatural feelings for a pig. Starts out with a opening scene that is a terrific example of absurd comedy. A formal orchestra audience is turned into an insane, violent mob by the crazy chantings of it's singers. Unfortunately it stays absurd the WHOLE time with no general narrative eventually making it just too off putting. Even those from the era should be turned off. The cryptic dialogue would make Shakespeare seem easy to a third grader. On a technical level it's better than you might think with some good cinematography by future great Vilmos Zsigmond. Future stars Sally Kirkland and Frederic Forrest can be seen briefly."]
# test_labels = ["neg"]
test_docs =list(test["text"])
test_docs=g_replace(test_docs)
test_labels =list(test["label"])
# 特征提取器
vectorizer = CountVectorizer()

# 将训练数据转换成词频矩阵
train_matrix = vectorizer.fit_transform(train_docs)

# 训练分类器

Log=LogisticRegression(C=10)
Log.fit(train_matrix,train_labels)


# 测试分类器
test_matrix = vectorizer.transform(test_docs)
predicted_labels = Log.predict(test_matrix)

# 输出结果
# print("Predicted labels:", predicted_labels)
#模型评估
print("准确率为：", Log.score(test_matrix, test_labels))
print("每个类别的精确率和召回率：", classification_report(test_labels, predicted_labels))